package datastructure.array;

import java.util.ArrayList;
import java.util.List;

/**
 * @author XY
 * @version 1.0
 * @date 2022/3/9 9:26
 * @Description
 */
public class Leet228 {

    public static List<String> summaryRanges(int[] nums) {
        List<String> list = new ArrayList<>();
        if(nums.length==0){
            return list;
        }
        int index = 0;
        for (int i = 1; i < nums.length; i++) {
            if(nums[i-1]+1!=nums[i]){
                if(nums[index]==nums[i-1]){
                    list.add(nums[index]+"");
                }else{
                    list.add(nums[index]+"->"+nums[i-1]);
                }
                index = i;
            }
        }
        //收集最后一组数据
        if(index==nums.length-1){
            list.add(nums[index]+"");
        }else{
            list.add(nums[index]+"->"+nums[nums.length-1]);
        }
        return list;
    }

    public static void main(String[] args) {
        int[] arr = {0,1,2,4,5,7};
        System.out.println(summaryRanges(arr));
    }
}
